package org.springblade.system.user.util;

import com.alibaba.fastjson.JSON;
import org.shy.core.sso.client.util.CallbackUtil;
import org.shy.core.sso.client.util.EncryptUtil;
import org.shy.core.sso.common.aes.AesException;
import org.springblade.applicationapi.entity.App;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * description: CallbackUtil <br>
 * date: 2022/4/7 8:59 <br>
 * version: 1.0 <br>
 * @author yzpang
 */
public class HttpUtil {
	public static void callback(String changeType, Object data, List<App> appList){
		Map<String, Object> map = new HashMap<>();
		map.put("changeType", changeType);
		if (changeType.endsWith("delete")){
			Map<String, Object> ids = new HashMap<>();
			ids.put("ids", data);
			map.put("data", JSON.toJSONString(ids));
		}else {
			map.put("data", JSON.toJSONString(data));
		}
		for (App app: appList){
			try {
				String encrypt = EncryptUtil.encryptMsg(app.getToken(), app.getAccessAesKey(),
					app.getAppId(), JSON.toJSONString(map));
				CallbackUtil.callback(encrypt, app.getCallbackUrl());
			} catch (AesException e) {
				e.printStackTrace();
			}
		}
	}
}
